﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
using System.Data;

namespace lcs.Web.supply
{
    public partial class AddWarnInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDL();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtmaterialInfo.Text.Trim().Length == 0)
            {
                strErr += "物料名称不能为空！\\n";
            }

            //if (!PageValidate.IsDecimal(txtminNums.Text))
            //{
            //    strErr += "最小预警数量格式错误！\\n";
            //}
            //if (!PageValidate.IsDecimal(txtmaxNums.Text))
            //{
            //    strErr += "最大预警数量格式错误！\\n";
            //}
            if (!PageValidate.IsNumber(txtwarnTime.Text))
            {
                strErr += "预警时间格式错误！\\n";
            }
            if (this.ddl_cguid.SelectedValue == "-1")
            {
                strErr += "请选择所属仓管员！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string materialInfo = this.txtmaterialInfo.Text;

            DataSet ds_m = new lcs.BLL.materialinfo().GetMaterial(" material= '" + materialInfo + "' ");

            if (ds_m.Tables[0].Rows.Count > 0)
            {
                decimal minNums = decimal.Parse(this.txtminNums.Text);
                decimal maxNums = decimal.Parse(this.txtmaxNums.Text);
                int warnTime = int.Parse(this.txtwarnTime.Text);
                int cguid = int.Parse(this.ddl_cguid.SelectedValue);

                Maticsoft.Model.warnInfo model = new Maticsoft.Model.warnInfo();
                model.material = materialInfo;
                model.minNums = minNums;
                model.maxNums = maxNums;
                model.warnTime = warnTime;
                model.cguid = cguid;

                Maticsoft.BLL.warnInfo bll = new Maticsoft.BLL.warnInfo();
                if (bll.Add(model) > 0)
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "ListWarnInfo.aspx");
                }
                
            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "仓库中没有此物料！");
            }

            

        }

        private void BindDDL()
        {
            DataSet ds = new lcs.BLL.user().GetList(" roles_id=2 ");//获取所有仓管员

            this.ddl_cguid.DataSource = ds;
            this.ddl_cguid.DataTextField = "user_name";
            this.ddl_cguid.DataValueField = "user_id";
            this.ddl_cguid.DataBind();

            this.ddl_cguid.Items.Insert(0, new ListItem("---未选择---","-1"));

    
        }
    }
}